<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Products extends CI_Controller {
    /* PAGES */

    /**
     * Constructeur de la class Groups.
     */
    public function __construct() {
        parent::__construct();

        $this->acl->setModule('groups');
    }

    /**
     * Page d'administration des produits.
     */
    public function admin() {
        $this->acl->can('manage');

        $crud = new grocery_CRUD();

        $crud->set_table('S_PRODUCTION.V_PRODUCT_TYPE_PDT')
                ->set_subject('un produit')

                // Champs.
                ->columns('PDT_NAME', 'PDT_STOCK_ALERT', 'PDT_COST')
                ->fields('PDT_NAME', 'PDT_DESCRIPTION', 'PDT_BARCODE', 'PDT_DURATION', 'PDT_STOCK_ALERT', 'PDT_COST')
                ->required_fields('PDT_NAME', 'PDT_DESCRIPTION', 'PDT_BARCODE', 'PDT_DURATION', 'PDT_STOCK_ALERT', 'PDT_COST')

                // Affichage.
                ->display_as('PDT_BARCODE', 'Code barre')
                ->display_as('PDT_COST', 'Cout')
                ->display_as('PDT_DESCRIPTION', 'Description')
                ->display_as('PDT_DURATION', 'Durée')
                ->display_as('PDT_NAME', 'Nom')
                ->display_as('PDT_STOCK_ALERT', 'Stock alerte')

        // Relations.
//                ->set_relation_n_n('USERS', 'S_INTRANET.T_J_GROUP_USER_GRU', 'S_BUSINESS_DATA.V_USER_USR', 'GRP_ID', 'USR_ID', '{USR_LAST_NAME} {USR_FIRST_NAME}', 'GRU_ID')
//                ->unset_texteditor('GRP_DESCRIPTION')

        ;
        $crud->order_by('PDT_NAME', 'ASC');

        // Callbacks
        $crud->callback_delete(array($this, 'callbackProductDelete'));
        $crud->callback_insert(array($this, 'callbackProductInsert'));
        $crud->callback_update(array($this, 'callbackProductUpdate'));

        $this->db = $this->load->database('DB_PLASTPROD', true);
        $output = $crud->render();

        $css = $crud->get_css_files();
        $js = $crud->get_js_files();

        $this->layout
                ->setTitle('Produits')
                ->setCss($css)
                ->setJs($js)
                ->view('products/admin', $output)
                ->display()
        ;
    }
    
        /**
     * Page d'administration des productions.
     */
    public function adminProductions() {
        $this->acl->can('manage');

        $crud = new grocery_CRUD();

        $crud->set_table('S_PRODUCTION.V_PRODUCTION_PRD')
                ->set_subject('une production')

                // Champs.
                
              //  "PRD_ID", "", "PDT_NAME", "PDT_BARCODE", "PCH_ID", "PRD_DATE", 
       //"PRD_SERIAL_NUMBER", "PRD_QUANTITY"
                
                ->columns('PDT_NAME', 'PDT_BARCODE', 'PCH_ID', 'PRD_DATE', 'PRD_QUANTITY')
                ->fields('PRT_ID', 'PRD_QUANTITY', 'PRD_SERIAL_NUMBER', 'PCH_ID', 'PRD_DATE')
                ->required_fields('PRT_ID', 'PRD_QUANTITY', 'PRD_SERIAL_NUMBER')

                // Affichage.
                ->display_as('PCH_ID', 'Chaine')
                ->display_as('PDT_BARCODE', 'Code barre')
                ->display_as('PDT_NAME', 'Nom')
                ->display_as('PRD_DATE', 'Date')
                ->display_as('PRD_QUANTITY', 'Quantité')
                ->display_as('PRD_SERIAL_NUMBER', 'Numéro de série')
                ->display_as('PRT_ID', 'Produit')

        // Relations.
        ->set_relation('PRT_ID', 'S_PRODUCTION.V_PRODUCT_TYPE_PDT', 'PDT_NAME')

        ;
        $crud->order_by('PRD_DATE', 'ASC');

        // Callbacks
        $crud->callback_delete(array($this, 'callbackProductionDelete'));
        $crud->callback_insert(array($this, 'callbackProductionInsert'));
        $crud->callback_update(array($this, 'callbackProductionUpdate'));

        $this->db = $this->load->database('DB_PLASTPROD', true);
        $output = $crud->render();

        $css = $crud->get_css_files();
        $js = $crud->get_js_files();

        $this->layout
                ->setTitle('Productions')
                ->setCss($css)
                ->setJs($js)
                ->view('products/adminProductions', $output)
                ->display()
        ;
    }

    /* CALLBACKS */

    /**
     * Callback de suppression d'un produit.
     * 
     * @param   int     $id
     * @return  boolean
     */
    public function callbackProductDelete($id) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                $id
            );

            $result = callProc('DB_PLASTPROD', 'S_PRODUCTION', 'P_D_PRODUCT_TYPE_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

    /**
     * Callback de création d'un produit.
     * 
     * @param   array   $post
     * @return  boolean
     */
    public function callbackProductInsert($post) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                '\'' . pg_escape_string($post['PDT_NAME']) . '\'',
                '\'' . pg_escape_string($post['PDT_DESCRIPTION']) . '\'',
                '\'' . pg_escape_string($post['PDT_BARCODE']) . '\'',
                $post['PDT_DURATION'],
                $post['PDT_STOCK_ALERT'],
                $post['PDT_COST']
            );

            $result = callProc('DB_PLASTPROD', 'S_PRODUCTION', 'P_I_PRODUCT_TYPE_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

    /**
     * Callback de mise à jour d'un produit.
     * 
     * @param   array   $post
     * @param   int     $id
     * @return  boolean
     */
    public function callbackProductUpdate($post, $id) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                $id,
                '\'' . pg_escape_string($post['PDT_NAME']) . '\'',
                '\'' . pg_escape_string($post['PDT_DESCRIPTION']) . '\'',
                '\'' . pg_escape_string($post['PDT_BARCODE']) . '\'',
                $post['PDT_DURATION'],
                $post['PDT_STOCK_ALERT'],
                $post['PDT_COST']
            );

            $result = callProc('DB_PLASTPROD', 'S_PRODUCTION', 'P_U_PRODUCT_TYPE_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

    /**
     * Callback de suppression d'une production.
     * 
     * @param   int     $id
     * @return  boolean
     */
    public function callbackProductionDelete($id) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                $id
            );

            $result = callProc('DB_PLASTPROD', 'S_PRODUCTION', 'P_D_PRODUCT_TYPE_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

    /**
     * Callback de création d'une production.
     * 
     * @param   array   $post
     * @return  boolean
     */
    public function callbackProductionInsert($post) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                '\'' . pg_escape_string($post['PDT_NAME']) . '\'',
                '\'' . pg_escape_string($post['PDT_DESCRIPTION']) . '\'',
                '\'' . pg_escape_string($post['PDT_BARCODE']) . '\'',
                $post['PDT_DURATION'],
                $post['PDT_STOCK_ALERT'],
                $post['PDT_COST']
            );

            $result = callProc('DB_PLASTPROD', 'S_PRODUCTION', 'P_I_PRODUCT_TYPE_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

    /**
     * Callback de mise à jour d'une production.
     * 
     * @param   array   $post
     * @param   int     $id
     * @return  boolean
     */
    public function callbackProductionUpdate($post, $id) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                $id,
                '\'' . pg_escape_string($post['PDT_NAME']) . '\'',
                '\'' . pg_escape_string($post['PDT_DESCRIPTION']) . '\'',
                '\'' . pg_escape_string($post['PDT_BARCODE']) . '\'',
                $post['PDT_DURATION'],
                $post['PDT_STOCK_ALERT'],
                $post['PDT_COST']
            );

            $result = callProc('DB_PLASTPROD', 'S_PRODUCTION', 'P_U_PRODUCT_TYPE_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }
}